package com.xscha.soyue.common.core.json.module;

import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.datatype.jsr310.deser.*;
import com.fasterxml.jackson.datatype.jsr310.ser.*;
import com.xscha.soyue.common.core.json.BeanJsonModule;
import java.time.*;
import java.time.format.DateTimeFormatter;

public class JavaTimeModule extends BeanJsonModule {

	private static final long serialVersionUID = -1718523688103305224L;

	@Override
	public void registerModels() {

		// ======================= 时间序列化规则 ===============================
		final DateTimeFormatter dateTime = DatePattern.NORM_DATETIME_FORMATTER;
		final DateTimeFormatter time = DateTimeFormatter.ISO_LOCAL_TIME;
		final DateTimeFormatter date = DateTimeFormatter.ISO_LOCAL_DATE;

		LocalDateTimeSerializer localDateTimeSerializer = new LocalDateTimeSerializer(dateTime);
		LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer(dateTime);

		addSerializer(LocalDate.class, new LocalDateSerializer(date))
			.addDeserializer(LocalDate.class, new LocalDateDeserializer(date))
			.addSerializer(LocalTime.class, new LocalTimeSerializer(time))
			.addDeserializer(LocalTime.class, new LocalTimeDeserializer(time))
			.addSerializer(LocalDateTime.class, localDateTimeSerializer)
			.addDeserializer(LocalDateTime.class, localDateTimeDeserializer)
			.addDeserializer(Duration.class, DurationDeserializer.INSTANCE)
			.addSerializer(Duration.class, DurationSerializer.INSTANCE)
			.addDeserializer(Instant.class, InstantDeserializer.INSTANT)
			.addSerializer(Instant.class, InstantSerializer.INSTANCE);
	}

}
